package jp.co.dailyReport.workItem.sql;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.workItem.WorkItem;

/**
 * 作業項目情報を取得するクラスです。
 */
public class WorkItemLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(WorkItemLoadExecuter.class);
	private int workItemId;

	public WorkItemLoadExecuter(int workItemId) {
		this.workItemId = workItemId;
	}

	/**
	 * 作業項目IDを基に、作業項目情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 作業項目オブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select * from workItem where workItemId=?";
		log.debug("sql: " + sql);
		Vector answer = new Vector();

		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, workItemId);
			rset = pstmt.executeQuery();
			WorkItem workItem;
			while(rset.next()) {
				workItem = new WorkItem();
				workItem.setWorkItemId(rset.getInt("workItemId"));
				workItem.setDisplayNm(rset.getInt("displayNm"));
				workItem.setProjectId(rset.getInt("projectId"));
				workItem.setName(Utilities.convUniqueCharacter(rset.getString("name")));
				workItem.setDeadline(Utilities.getCalendar(rset.getDate("deadline")));
				workItem.setAddStaffId(rset.getInt("addStaffId"));
				workItem.setUnitPrice(rset.getInt("unitPrice"));
				workItem.setDelFlg(Utilities.decimalToBoolean(rset.getInt("delFlg")));
				workItem.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
				answer.add(workItem);
			}

		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (WorkItem[])answer.toArray(new WorkItem[0]);
	}

}
